package com.shuang.springweb.model.util;

import java.lang.reflect.Field;

public class ReflectTest {

	public static void main(String[] args) throws ClassNotFoundException {
		Class<?> c = Class.forName("com.shuang.springweb.model.User");
		createtoStringMethod(c);
	}

	public static void createtoStringMethod(Class c) {
		
		String header = "public String toString() { return \"[\"+ super.toString()+\"] : <\"";
		StringBuffer buffer = new StringBuffer(header);

		Field[] fields = c.getDeclaredFields();
		//buffer.append("[");
		for (Field f : fields) {
			buffer.append("+\"[" + f.getName() + "=\"+" + f.getName()+"+\"];\"");
		}
		
		buffer.deleteCharAt(buffer.length()-2);
		buffer.append("+\">\"");
		buffer.append(";}");
		System.out.println(buffer.toString());
	}

}
